/*************************************************************************
	> File Name: testFW.c
	> Author: shubulan
	> Mail: gaoyulong1996@163.com
	> Created Time: Fri 16 Oct 2020 06:53:09 PM CST
 ************************************************************************/
#include <include/testFW.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <malloc.h>
static FuncStruc FuncHead, *tail = &FuncHead; 

void add_func(FuncType func, const char * funcname) {
    FuncStruc *temp = (FuncStruc *) calloc(1, sizeof(FuncStruc));
    temp->func = func;
    temp->funcName = strdup(funcname);
    tail->next.next = &temp->next; 
    tail = temp;
}
struct funcInfo testInfo;
int RUN_ALL_TESTS() {
    for (struct LinkNode *p = FuncHead.next.next; p; p = p->next) {
        FuncStruc *func = GETDATA(p, FuncStruc, next);
        testInfo.total = 0, testInfo.success = 0;
        printf(GREEN("[====RUN====] ") HRED("%s") "\n" , func->funcName);
        func->func();
        double rate = 100.0 * testInfo.success / testInfo.total; 
        printf(GREEN("[  "));
        if (fabs(rate - 100.0) < 1e-6) {
            printf(HBLUE("%-6.2lf%%"), rate);
        } else {
            printf(HRED("%-6.2lf%%"), rate);
        }
        printf(GREEN("  ]") " sucess : %d  failed : %d\n", testInfo.total, testInfo.success); 
    }
    struct LinkNode *p = FuncHead.next.next, *tofree;
    
    while (p) {
        tofree = p; 
        p = p->next;
        FuncStruc *fc = GETDATA(tofree, FuncStruc, next);
        free(fc);
    }
    return 0;
}
